{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/form/fields_macros.html.twig' as fields %}

{% set no_header = no_header|default(not item.isNewItem() and not _get._in_modal|default(false)) %}
{% set bg = '' %}
{% if item.isDeleted() %}
   {% set bg = 'asset-deleted' %}
{% endif %}

<div class="asset {{ bg }}">
   {{ include('components/form/header.html.twig', {'in_twig': true}) }}

   {% set rand = random() %}
   {% set params  = params ?? [] %}
   {% set target       = params['target'] ?? item.getFormURL() %}
   {% set withtemplate = params['withtemplate'] ?? '' %}
   {% set item_type = item.getType() %}
   {% set item_has_pictures = item.hasItemtypeOrModelPictures() %}
   {% set field_options = {
       'locked_fields': item.getLockedFields(),
       'rand': rand,
   } %}

    {# Support custom fields for generic assets #}
    {% set custom_fields = custom_fields|default({}) %}
    {% set field_order = field_order|default(item.getFormFields()) %}
    {% set has_third_col = item_has_pictures or (not item.isNewItem() and item_type in config("asset_types")) %}

   <div class="card-body d-flex flex-wrap">
      <div class="col-12 col-xxl-{{ has_third_col ? '9' : '12' }} flex-column">
         <div class="d-flex flex-row flex-wrap flex-xl-nowrap">
            <div class="row flex-row align-items-start flex-grow-1" style="min-width: 0;">
               <div class="row flex-row">
                  {% block form_fields %}
                      {% for field in field_order %}
                          {% set specific_field_options = field_options|merge(additional_field_options[field]|default({})) %}

                          {% if field is same as 'name' %}
                              {{ fields.autoNameField('name', item, (item_type == 'Contact' ? __('Surname') : __('Name')), withtemplate, specific_field_options) }}
                          {% elseif field is same as 'firstname' %}
                              {{ fields.autoNameField('firstname', item, __('First name'), withtemplate, specific_field_options) }}
                          {% elseif field is same as 'template_name' and withtemplate == 1 and no_header %}
                              {{ fields.autoNameField('template_name', item, __('Template name'), 0, specific_field_options) }}
                          {% elseif item.isField('is_active') and field is same as '_template_is_active' and withtemplate >= 1 %}
                              {{ fields.hiddenField('is_active', item.fields['is_active'], __('Is active'), {
                                  'add_field_html': __('No')
                              }) }}
                          {% elseif field is same as 'states_id' and item is usingtrait('Glpi\\Features\\State') %}
                              {{ fields.dropdownField(
                                  'State',
                                  'states_id',
                                  item.fields['states_id'],
                                  __('Status'),
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                      'condition': item.getStateVisibilityCriteria()
                                  })
                              ) }}
                          {% elseif field is same as item.getForeignKeyField() and item_type != 'Software' %}
                              {{ fields.dropdownField(
                                  item_type,
                                  field,
                                  item.fields[field],
                                  __('As child of'),
                                  specific_field_options|merge({
                                      entity: item.fields['entities_id']|default(-1),
                                      used: [item.getID()]
                                  })
                              ) }}
                          {% elseif field is same as 'is_helpdesk_visible' and item_type != 'SoftwareLicense' %}
                              {# TODO Drop unused 'is_helpdesk_visible' field in SoftwareLicense schema? #}
                              {{ fields.checkboxField(
                                  'is_helpdesk_visible',
                                  item.fields['is_helpdesk_visible'],
                                  __('Associable to a ticket'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as '_dc_breadcrumbs' %}
                              {% set conditions_met = 0 %}
                              {% set dc_breadcrumbs %}
                                  {% if item is usingtrait('Glpi\\Features\\DCBreadcrumb') %}
                                      {{ call(item.getType() ~ '::renderDcBreadcrumb', [
                                          item.getID
                                      ])|raw }}
                                  {% endif %}
                              {% endset %}
                              {% if dc_breadcrumbs|trim|length > 0 %}
                                  {{ fields.htmlField(
                                      '',
                                      dc_breadcrumbs,
                                      __('Data center position'),
                                  ) }}
                                  {% set conditions_met = conditions_met + 1 %}
                              {% endif %}

                              {% if cluster is defined and cluster is not null %}
                                  {{ fields.htmlField(
                                      '',
                                      cluster.getLink(),
                                      _n('Cluster', 'Clusters', 1),
                                  ) }}
                                  {% set conditions_met = conditions_met + 1 %}
                              {% endif %}
                              {% if conditions_met == 1 %}
                                  {{ fields.nullField() }}
                              {% endif %}
                          {% elseif field is same as 'locations_id' %}
                              {{ fields.dropdownField(
                                  'Location',
                                  'locations_id',
                                  item.fields['locations_id'],
                                  'Location'|itemtype_name,
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                  })
                              ) }}
                          {% elseif field is same as 'item_type' and item_type == 'Unmanaged' %}
                              {{ fields.dropdownArrayField(
                                  'item_type',
                                  item.fields['itemtype'],
                                  _n('Type', 'Types', 1),
                                  [
                                      'Computer', 'NetworkEquipment', 'Printer', 'Peripheral', 'Phone'
                                  ],
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'itemtype' and item_type == 'DefaultFilter' %}
                              {{ fields.dropdownItemTypes(
                                  'itemtype',
                                  item.fields['itemtype'],
                                  __('Itemtype'),
                                  specific_field_options|merge({'types': config('globalsearch_types')})
                              ) }}
                          {% elseif field is same as 'date_domaincreation' %}
                              {{ fields.datetimeField('date_domaincreation', item.fields['date_domaincreation'], __('Registration date')) }}
                          {% elseif field is same as item.getTypeForeignKeyField() %}
                              {% set type_itemtype = item.getTypeClass() %}
                              {{ fields.dropdownField(
                                  type_itemtype,
                                  field,
                                  item.fields[field],
                                  type_itemtype|itemtype_name,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'usertitles_id' %}
                              {{ fields.dropdownField(
                                  'UserTitle',
                                  'usertitles_id',
                                  item.fields['usertitles_id'],
                                  'UserTitle'|itemtype_name,
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                  })
                              ) }}
                          {% elseif field is same as 'registration_number' %}
                              {{ fields.autoNameField(
                                  'registration_number',
                                  item,
                                  (item_type starts with 'User' ? _x('user', 'Administrative number') : _x('infocom', 'Administrative number')),
                              withtemplate,
                              specific_field_options
                              ) }}
                          {% elseif field is same as 'phone' %}
                              {{ fields.autoNameField(
                                  'phone',
                                  item,
                                  'Phone'|itemtype_name,
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'phone2' %}
                              {{ fields.autoNameField(
                                  'phone2',
                                  item,
                                  __('Phone 2'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'phonenumber' %}
                              {{ fields.autoNameField(
                                  'phonenumber',
                                  item,
                                  'Phone'|itemtype_name,
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'mobile' %}
                              {{ fields.autoNameField(
                                  'mobile',
                                  item,
                                  __('Mobile phone'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'fax' %}
                              {{ fields.autoNameField(
                                  'fax',
                                  item,
                                  __('Fax'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'website' %}
                              {{ fields.autoNameField(
                                  'website',
                                  item,
                                  __('Website'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'email' %}
                              {{ fields.autoNameField(
                                  'email',
                                  item,
                                  _n('Email', 'Emails', 1),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'address' %}
                              {{ fields.textareaField('address', item.fields['address'], __('Address')) }}
                          {% elseif field is same as 'town' %}
                              {{ fields.autoNameField(
                                  'town',
                                  item,
                                  __('City'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'postcode' %}
                              {{ fields.autoNameField(
                                  'postcode',
                                  item,
                                  __('Postal code'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'state' and (item_type == 'Supplier' or item_type == 'Contact') %}
                              {{ fields.autoNameField(
                                  'state',
                                  item,
                                  _x('location', 'State'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'country' %}
                              {{ fields.autoNameField(
                                  'country',
                                  item,
                                  __('Country'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'date_expiration' %}
                              {% if item_type == 'Certificate' %}
                                  {{ fields.dateField('date_expiration', item.fields['date_expiration'], __('Expiration date'), {
                                      'helper': __('Empty for infinite'),
                                      'checkIsExpired': false,
                                      'expiration_class' : params.expiration_class
                                  }|merge(specific_field_options)) }}
                              {% elseif item_type == 'ItemAntivirus' %}
                                  {{ fields.dateField('date_expiration', item.fields['date_expiration'], __('Expiration date'), {
                                      'helper': __('Empty for infinite'),
                                      'checkIsExpired': true,
                                  }|merge(specific_field_options)) }}
                              {% else %}
                                  {{ fields.datetimeField('date_expiration', item.fields['date_expiration'], __('Expiration date'), {
                                      'helper': __('Empty for infinite'),
                                      'checkIsExpired': true
                                  }|merge(specific_field_options)) }}
                              {% endif %}
                          {% elseif field is same as 'ref' %}
                              {{ fields.textField(
                                  'ref',
                                  item.fields['ref'],
                                  __('Reference'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'users_id_tech' %}
                              {{ fields.dropdownField(
                                  'User',
                                  'users_id_tech',
                                  item.fields['users_id_tech'],
                                  __('Technician in charge'),
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                      'right': 'own_ticket',
                                  })
                              ) }}
                          {% elseif field is same as 'manufacturers_id' %}
                              {{ fields.dropdownField(
                                  'Manufacturer',
                                  'manufacturers_id',
                                  item.fields['manufacturers_id'],
                                  (item_type starts with 'Software' ? __('Publisher') : 'Manufacturer'|itemtype_name),
                              specific_field_options
                              ) }}
                          {% elseif field is same as 'groups_id_tech' %}
                              {{ fields.dropdownField(
                                  'Group',
                                  'groups_id_tech',
                                  item.fields['groups_id_tech'],
                                  __('Group in charge'),
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                      'condition': {'is_assign': 1},
                                      'multiple': item is usingtrait('Glpi\\Features\\AssignableItem') ? true : false
                                  })
                              ) }}
                          {% elseif field is same as item.getModelForeignKeyField() %}
                              {% set model_itemtype = item.getModelClass() %}
                              {{ fields.dropdownField(
                                  model_itemtype,
                                  field,
                                  item.fields[field],
                                  _n('Model', 'Models', 1),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'contact_num' and item_type != 'SoftwareLicense' %}
                              {# TODO Drop unused 'contact_num' field in Software License schema? #}
                              {{ fields.textField(
                                  'contact_num',
                                  item.fields['contact_num'],
                                  __('Alternate username number'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'serial' %}
                              {{ fields.textField(
                                  'serial',
                                  item.fields['serial'],
                                  __('Serial number'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'contact' and item_type != 'SoftwareLicense' %}
                              {# TODO Drop unused 'contact' field in Software License schema? #}
                              {{ fields.textField(
                                  'contact',
                                  item.fields['contact'],
                                  __('Alternate username'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'otherserial' %}
                              {{ fields.autoNameField(
                                  'otherserial',
                                  item,
                                  __('Inventory number'),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'sysdescr' %}
                              {{ fields.textareaField('sysdescr', item.fields['sysdescr'], __('Sysdescr'), specific_field_options) }}
                          {% elseif field is same as 'snmpcredentials_id' %}
                              {{ fields.dropdownField(
                                  'SNMPCredential',
                                  'snmpcredentials_id',
                                  item.fields['snmpcredentials_id'],
                                  'SNMPCredential'|itemtype_name,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'users_id' %}
                              {{ fields.dropdownField(
                                  'User',
                                  'users_id',
                                  item.fields['users_id'],
                                  'User'|itemtype_name,
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                      'right': 'all',
                                  })
                              ) }}
                          {% elseif field is same as 'is_global' %}
                              {% set management_restrict = 0 %}
                              {% if item_type == 'Monitor' %}
                                  {% set management_restrict = config('monitors_management_restrict') %}
                              {% elseif item_type == 'Peripheral' %}
                                  {% set management_restrict = config('peripherals_management_restrict') %}
                              {% elseif item_type == 'Phone' %}
                                  {% set management_restrict = config('phones_management_restrict') %}
                              {% elseif item_type == 'Printer' %}
                                  {% set management_restrict = config('printers_management_restrict') %}
                              {% else %}
                                  {% set management_restrict = 0 %}
                              {% endif %}
                              {% set dd_globalswitch %}
                                  {% do call('Dropdown::showGlobalSwitch', [item.fields['id'], {
                                      'withtemplate': withtemplate,
                                      'value': item.fields['is_global'],
                                      'management_restrict': management_restrict,
                                      'target': target,
                                      'width': '100%',
                                  }]) %}
                              {% endset %}
                              {{ fields.htmlField(
                                  'is_global',
                                  dd_globalswitch,
                                  __('Management type'),
                              ) }}
                          {% elseif field is same as 'size' %}
                              {{ fields.numberField(
                                  'size',
                                  item.fields['size'],
                                  __('Size'),
                                  specific_field_options|merge({
                                      'min': 0,
                                      'step': 0.01
                                  }),
                              ) }}
                          {% elseif field is same as 'networks_id' %}
                              {{ fields.dropdownField(
                                  'Network',
                                  'networks_id',
                                  item.fields['networks_id'],
                                  _n('Network', 'Networks', 1),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'groups_id' %}
                              {{ fields.dropdownField(
                                  'Group',
                                  'groups_id',
                                  item.fields['groups_id'],
                                  'Group'|itemtype_name,
                                  specific_field_options|merge({
                                      'entity': item.fields['entities_id']|default(-1),
                                      'condition': {'is_itemgroup': 1},
                                      'multiple': item is usingtrait('Glpi\\Features\\AssignableItem') ? true : false
                                  })
                              ) }}
                          {% elseif field is same as 'uuid' %}
                              {{ fields.textField(
                                  'uuid',
                                  item.fields['uuid'],
                                  __('UUID'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'version' %}
                              {{ fields.autoNameField(
                                  'version',
                                  item,
                                  _n('Version', 'Versions', 1),
                                  withtemplate,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'comment' %}
                              {{ fields.textareaField(
                                  'comment',
                                  item.fields['comment'],
                                  _n('Comment', 'Comments', get_plural_number()),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'ram' %}
                              {{ fields.numberField(
                                  'ram',
                                  item.fields['ram'],
                                  __('%1$s (%2$s)')|format(_n('Memory', 'Memories', 1), __('Mio')),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'alarm_threshold' %}
                              {% set dd_alarm_treshold %}
                                  {% do call('Dropdown::showNumber', ['alarm_threshold', {
                                      'value': item.fields['alarm_threshold'],
                                      'rand': rand,
                                      'width': '100%',
                                      'min': 0,
                                      'max': 100,
                                      'step': 1,
                                      'toadd': {'-1': __('Never')}
                                  }|merge(params)]) %}
                              {% endset %}
                              {% set last_alert_html %}
                                  <span class="text-muted">
                                      {% do call('Alert::displayLastAlert', [item_type, item.fields['id']]) %}
                                  </span>
                              {% endset %}
                              {{ fields.htmlField(
                                  'alarm_threshold',
                                  dd_alarm_treshold,
                                  __('Alert threshold'),
                                  specific_field_options|merge({
                                      add_field_html: last_alert_html
                                  })
                              ) }}
                          {% elseif field is same as 'brand' %}
                              {{ fields.textField(
                                  'brand',
                                  item.fields['brand'],
                                  __('Brand'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'begin_date' %}
                              {{ fields.dateField(
                                  'begin_date',
                                  item.fields['begin_date'],
                                  __('Start date'),
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'autoupdatesystems_id' %}
                              {{ fields.dropdownField(
                                  'AutoUpdateSystem',
                                  'autoupdatesystems_id',
                                  item.fields['autoupdatesystems_id'],
                                  'AutoUpdateSystem'|itemtype_name,
                                  specific_field_options
                              ) }}
                          {% elseif field is same as 'pictures' %}
                              {{ fields.fileField('pictures', null, _n('Picture', 'Pictures', get_plural_number()), {
                                  'onlyimages': true,
                                  'multiple': true,
                              }) }}
                          {% elseif field is same as 'is_active' %}
                              {{ fields.dropdownYesNo('is_active', item.fields['is_active'], __('Active'), specific_field_options) }}
                          {% elseif field is same as 'last_boot' and item.isField('is_dynamic') and item.fields['is_dynamic'] %}
                              {# display last_boot data only if item is dynamic and have field #}
                              {{ fields.htmlField('last_boot', item.fields['last_boot']|formatted_datetime(true), __('Last boot date')) }}
                          {% elseif field in custom_fields|keys %}
                              {{ custom_fields[field].getFieldType().getFormInput(field, item.fields[field])|raw }}
                          {% endif %}
                      {% endfor %}

                     {% block more_fields %}
                     {% endblock %}
                  {% endblock %}
               </div> {# .row #}
            </div> {# .row #}
         </div> {# .flex-row #}
      </div>
       <div class="col-12 col-xxl-3 flex-column">
           <div class="flex-row asset-pictures">
               {{ include('components/form/pictures.html.twig', {'gallery_type': ''}) }}
           </div>
       </div>
   </div> {# .card-body #}

   {% if item_type == 'Contract' %}
      {{ include('components/form/support_hours.html.twig') }}
   {% endif %}
   {% if no_form_buttons is not defined or no_form_buttons == false %}
      {{ include('components/form/buttons.html.twig') }}
   {% endif %}
   {% if no_inventory_footer is not defined or no_inventory_footer == false %}
      {{ include('components/form/inventory_info.html.twig') }}
   {% endif %}

   {% if params['formfooter'] ?? true %}
      <div class="card-footer mx-n2 mb-n2">
         {{ include('components/form/dates.html.twig') }}
      </div>
   {% endif %}
</div>
